package com.ruoyi.useradmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.useradmin.mapper.UaAttentionMapper;
import com.ruoyi.useradmin.service.IUaAttentionService;
import com.ruoyi.useradmin.domain.UaAttention;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.useradmin.domain.UaUser;
import com.ruoyi.useradmin.service.IUaUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UaAttentionServiceImpl implements IUaAttentionService {


    @Resource
    private UaAttentionMapper uaAttentionMapper;
    
    
    @Resource
    private IUaUserService uaUserService;
	@Resource
    private IUaUserService uaUser2Service;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param uaAttention 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<UaAttention> getUaAttentionPage(PageDomain pageDomain, UaAttention uaAttention , UaUser uaUser2) {
        if (Objects.isNull(uaAttention)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<UaAttention> queryWrapper = new LambdaQueryWrapper<>(new UaAttention());
            //if(!Objects.isNull(uaAttention.getName())){
            //	queryWrapper.like(UaAttention::getName,uaAttention.getName());
            //}
            

            if (!Objects.isNull(uaAttention.getId())) {
                queryWrapper.eq(UaAttention::getId, uaAttention.getId());
            }

            if (!Objects.isNull(uaAttention.getUaUserId())) {
                queryWrapper.eq(UaAttention::getUaUserId, uaAttention.getUaUserId());
            }

            if (!Objects.isNull(uaAttention.getUaUser2Id())) {
                queryWrapper.eq(UaAttention::getUaUser2Id, uaAttention.getUaUser2Id());
            }
            uaUser2.setBaseEntityNull();
            if (!PageDomain.checkObjAllFieldsIsNull(uaUser2)) {
                List<Long> idList = uaUser2Service.getIdListByUaUser(uaUser2);
                queryWrapper.in(UaAttention::getUaUser2Id, idList);
            }

            if (!Objects.isNull(uaAttention.getStatus())) {
                queryWrapper.like(UaAttention::getStatus, uaAttention.getStatus());
            }

            if (!Objects.isNull(uaAttention.getDelFlag())) {
                queryWrapper.like(UaAttention::getDelFlag, uaAttention.getDelFlag());
            }

            if (!Objects.isNull(uaAttention.getCreateBy())) {
                queryWrapper.like(UaAttention::getCreateBy, uaAttention.getCreateBy());
            }

            if (!Objects.isNull(uaAttention.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(uaAttention.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(uaAttention.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(UaAttention::getCreateTime, uaAttention.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(uaAttention.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(UaAttention::getCreateTime, uaAttention.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(uaAttention.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(uaAttention.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(UaAttention::getCreateTime, uaAttention.getParams().get("CreateTimeBeginTime"), uaAttention.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaAttention.getUpdateBy())) {
                queryWrapper.like(UaAttention::getUpdateBy, uaAttention.getUpdateBy());
            }

            if (!Objects.isNull(uaAttention.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(uaAttention.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(uaAttention.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(UaAttention::getUpdateTime, uaAttention.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(uaAttention.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(UaAttention::getUpdateTime, uaAttention.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(uaAttention.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(uaAttention.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(UaAttention::getUpdateTime, uaAttention.getParams().get("UpdateTimeBeginTime"), uaAttention.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaAttention.getRemark())) {
                queryWrapper.like(UaAttention::getRemark, uaAttention.getRemark());
            }
        Page<UaAttention> uaAttentionList= uaAttentionMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(UaAttention record:uaAttentionList.getRecords()){
            record.setUaUser(uaUserService.getUaUserById(record.getUaUserId()));
            record.setUaUser2(uaUser2Service.getUaUserById(record.getUaUser2Id()));
        }

            return uaAttentionList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public UaAttention getUaAttentionById(Long id) {
        if (Objects.isNull(id)) {
            return new UaAttention();
        }
        try {
            UaAttention uaAttention = uaAttentionMapper.selectById(id);
            uaAttention.setUaUser(uaUserService.getUaUserById(uaAttention.getUaUserId()));
            uaAttention.setUaUser2(uaUser2Service.getUaUserById(uaAttention.getUaUser2Id()));
            if (Objects.isNull(uaAttention)){
                uaAttention = new UaAttention();
            }
            return uaAttention;

        } catch (Exception e) {
            e.printStackTrace();
            return new UaAttention();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param uaAttention 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByUaAttention(UaAttention uaAttention) {
        if (Objects.isNull(uaAttention)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<UaAttention> queryWrapper = new LambdaQueryWrapper<>(new UaAttention());
            

            if (!Objects.isNull(uaAttention.getId())) {
                queryWrapper.eq(UaAttention::getId, uaAttention.getId());
            }

            if (!Objects.isNull(uaAttention.getUaUserId())) {
                queryWrapper.eq(UaAttention::getUaUserId, uaAttention.getUaUserId());
            }

            if (!Objects.isNull(uaAttention.getUaUser2Id())) {
                queryWrapper.eq(UaAttention::getUaUser2Id, uaAttention.getUaUser2Id());
            }

            if (!Objects.isNull(uaAttention.getStatus())) {
                queryWrapper.like(UaAttention::getStatus, uaAttention.getStatus());
            }

            if (!Objects.isNull(uaAttention.getDelFlag())) {
                queryWrapper.like(UaAttention::getDelFlag, uaAttention.getDelFlag());
            }

            if (!Objects.isNull(uaAttention.getCreateBy())) {
                queryWrapper.like(UaAttention::getCreateBy, uaAttention.getCreateBy());
            }

            if (!Objects.isNull(uaAttention.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(uaAttention.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(uaAttention.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(UaAttention::getCreateTime, uaAttention.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(uaAttention.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(UaAttention::getCreateTime, uaAttention.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(uaAttention.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(uaAttention.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(UaAttention::getCreateTime, uaAttention.getParams().get("CreateTimeBeginTime"), uaAttention.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaAttention.getUpdateBy())) {
                queryWrapper.like(UaAttention::getUpdateBy, uaAttention.getUpdateBy());
            }

            if (!Objects.isNull(uaAttention.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(uaAttention.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(uaAttention.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(UaAttention::getUpdateTime, uaAttention.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(uaAttention.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(UaAttention::getUpdateTime, uaAttention.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(uaAttention.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(uaAttention.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(UaAttention::getUpdateTime, uaAttention.getParams().get("UpdateTimeBeginTime"), uaAttention.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaAttention.getRemark())) {
                queryWrapper.like(UaAttention::getRemark, uaAttention.getRemark());
            }

            List<UaAttention> uaAttentions = uaAttentionMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (UaAttention uaAttentionNo:uaAttentions){
                idList.add(uaAttentionNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过uaUser外键idList查询idList
     *
     * @param uaUserIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByUaUserIdList(List<Long> uaUserIdList) {
        if (Objects.isNull(uaUserIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<UaAttention> queryWrapper = new LambdaQueryWrapper<>(new UaAttention());
            queryWrapper.in(UaAttention::getUaUserId, uaUserIdList);
            List<UaAttention> uaAttentions = uaAttentionMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (UaAttention uaAttention:uaAttentions){
                idList.add(uaAttention.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }


    /**
     * 通过uaUser2外键idList查询idList
     *
     * @param uaUser2IdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByUaUser2IdList(List<Long> uaUser2IdList) {
        if (Objects.isNull(uaUser2IdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<UaAttention> queryWrapper = new LambdaQueryWrapper<>(new UaAttention());
            queryWrapper.in(UaAttention::getUaUser2Id, uaUser2IdList);
            List<UaAttention> uaAttentions = uaAttentionMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (UaAttention uaAttention:uaAttentions){
                idList.add(uaAttention.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param uaAttention 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertUaAttention(UaAttention uaAttention) {
        if (Objects.isNull(uaAttention)) {
            return -1;
        }
        try {
        
            //uaAttention.setSpell(PinYin.getPinYin(uaAttention.getName()));
            return uaAttentionMapper.insert(uaAttention);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param uaAttention 实体类
     * @return 结果
     */
    @Override
    public int updateUaAttentionById(UaAttention uaAttention) {
        if (Objects.isNull(uaAttention)) {
            return -1;
        }
        try {
            //uaAttention.setSpell(PinYin.getPinYin(uaAttention.getName()));
            return uaAttentionMapper.updateById(uaAttention);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteUaAttentionByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return uaAttentionMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<UaAttention> getUaAttentionList() {
        try {
            LambdaQueryWrapper<UaAttention> queryWrapper = new LambdaQueryWrapper<>(new UaAttention());
            queryWrapper.isNotNull(UaAttention::getId);
            return uaAttentionMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
